package com.deeper.customview.ui.c5.demo.c05d02;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.deeper.customview.R;

public class CircleImageView extends AppCompatImageView {
    private int color;
    private int border;
    private Xfermode xfermode;
    private Paint paint;
    private Path path;


    public CircleImageView(Context context) {
        super(context);
    }


    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);

        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        path = new Path();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
        border = typedArray.getDimensionPixelSize(R.styleable.CircleImageView_circle_border, 0);
        color = typedArray.getColor(R.styleable.CircleImageView_circle_color, Color.GRAY);

        typedArray.recycle();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Drawable mDrawable = getDrawable();
        if (mDrawable == null) {
            super.onDraw(canvas);
        }

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        RectF ovalRect = new RectF(0, 0, width, height);
        //在按照教程执行时，在 Android 6.0的系统中能正常显示，
        // 但在Android 10.0的系统中出现异常，初步判定为版本兼容性
        //因此这里采用原始办法，先绘制在一个Bitmap上，然后在进行混合运算
        //在 C04D10 中也采用了同样方式。不确定是否存在过度绘制问题
        Bitmap srcBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas srcCanvas = new Canvas(srcBmp);
        path.reset();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        path.addOval(ovalRect, Path.Direction.CCW);
        srcCanvas.drawPath(path, paint);

        int layer = canvas.saveLayer(getPaddingLeft(), getPaddingTop(), width, height, null);
        Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
        Matrix matrix = new Matrix();
        matrix.setScale(2, 2);
        canvas.setMatrix(matrix);
        canvas.drawBitmap(bitmap, new Rect(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight()), ovalRect, null);

        matrix.setScale(1, 1);
        canvas.setMatrix(matrix);
        paint.setXfermode(xfermode);
        canvas.drawBitmap(srcBmp, 0, 0, paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layer);

        if (border != 0) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(color);
            paint.setStrokeWidth(border);
            ovalRect.inset(border / 2, border / 2);
            canvas.drawOval(ovalRect, paint);
        }
    }
}
